<?php

/**
 * 开放平台基础类文件 授权及消息通知的实现
 */

namespace app\common\helper\upload;

use Qcloud\Cos\Client;

class UploadQcloudHandler {

    private $config;
    private $url;

    public function __construct($config) {
        $this->config = $config;
        $this->url = rtrim($config['domain'], '\/');
    }

    public function upload($file, $name) {
        $secretId = trim($this->config['qcloud']['secret_id']); //"云 API 密钥 SecretId";
        $secretKey = trim($this->config['qcloud']['secret_key']); //"云 API 密钥 SecretKey";
        $region = trim($this->config['qcloud']['region']); //设置一个默认的存储桶地域
        $cosClient = new Client([
            'region' => $region,
            'schema' => 'https', //协议头部，默认为http
            'credentials' => [
                'secretId' => $secretId,
                'secretKey' => $secretKey
            ]
        ]);
        $result = $cosClient->putObject([
            'Bucket' => trim($this->config['qcloud']['bucket']), //格式：BucketName-APPID 
            'Key' => $name,
            'Body' => fopen($file['tmp_name'], 'rb'),
        ]);
        return [
            'url' => $this->url . '/' . $name,
            'key' => $name,
            'hash' => $result['RequestId'], //其实应该md5 file的
        ];
    }

    public function delete() {
        
    }

}
